package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBxda;
import cn.stylefeng.guns.sys.modular.system.entity.ZxBxMingxi;
import cn.stylefeng.guns.sys.modular.system.model.params.ZxBxMingxiParam;
import cn.stylefeng.guns.sys.modular.system.model.params.ZxChengbenParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyBxdaResult;
import cn.stylefeng.guns.sys.modular.system.model.result.ZxTzqkViewResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyBxdaService;
import cn.stylefeng.guns.sys.modular.system.service.ZxBxMingxiService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ErrorResponseData;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;


/**
 * 控制器
 *
 * @author modular/system
 * @Date 2020-11-19 14:18:37
 */
@Controller
@RequestMapping("/dgdzxBxMingxi")
public class DgdZxBxMingxiController extends BaseController {

    private Long pinpai = 1417361607353536514L;

    private String PREFIX = "/modular/system/dgdzxBxMingxi";

    @Resource
    private ZxBxMingxiService zxBxMingxiService;

    @Resource
    private BtyBxdaService btyBxdaService;
    /**
     * 跳转到主页面
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/zxChengbenMingxi.html";
    }
    /**
     * 跳转到主页面
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("shenhe_index")
    public String shenhe_index() {
        return PREFIX + "/zxChengbenMingxi_shenhe_index.html";
    }

    /**
     * 跳转到选择货品页面
     * @author liuy
     * @Date
     */
    @RequestMapping("/index_huopin")
    public String index_huopin(Model model, ZxChengbenParam param) {
        //从成本设置跳转过来时，传递门店的id
        model.addAttribute("btyZxMdzxId", param.getBtyZxMdzxId());
        model.addAttribute("zhaiyao", param.getZhaiyao());
        model.addAttribute("beizhu", param.getBeizhu());
        model.addAttribute("riqi", param.getRiqi());
        //当前用户
        model.addAttribute("curuslister", LoginContextHolder.getContext().getUser());
        //获取报销档案
        String xingming =  LoginContextHolder.getContext().getUser().getName();
        String account =  LoginContextHolder.getContext().getUser().getAccount();
        BtyBxdaResult bxda = btyBxdaService.getByTel("半天妖",account);
        if(bxda ==null){
            BtyBxda mtp = new BtyBxda();
            mtp.setTel(LoginContextHolder.getContext().getUser().getAccount());
            mtp.setXm(xingming);
            mtp.setDm("半天妖");
            btyBxdaService.add(mtp);

            bxda = new BtyBxdaResult();
            bxda.setId(String.valueOf(mtp.getId()));
            bxda.setXm(xingming);
            bxda.setTel(account);
        }
//        if(!bxda.getXm().equals(xingming)){
//            BtyBxda bxdau = btyBxdaService.getById(bxda.getId());
//            bxdau.setXm(xingming);
//            btyBxdaService.updateById(bxdau);
//            bxda.setXm(xingming);
//        }
        model.addAttribute("bxda", bxda);
        return PREFIX + "/huopin.html";
    }

    /**
     * 跳转到主页面-直接添加报销明细页面
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/zxChengbenMingxiNew")
    public String index2(Model model) {
        model.addAttribute("curuser", LoginContextHolder.getContext().getUser());
        model.addAttribute("ckcheck", "");
        return PREFIX + "/zxChengbenMingxi_new.html";
    }

    /**
     * 跳转到主页面-直接添加报销明细页面
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/zxChengbenMingxiGhs")
    public String indexGhs(Model model) {
        //当前用户
        model.addAttribute("curuslister", LoginContextHolder.getContext().getUser());
        //获取报销档案
        String xingming =  LoginContextHolder.getContext().getUser().getName();
        String account =  LoginContextHolder.getContext().getUser().getAccount();
        BtyBxdaResult bxda = btyBxdaService.getByTel("半天妖",account);
        if(bxda ==null){
            BtyBxda mtp = new BtyBxda();
            mtp.setTel(LoginContextHolder.getContext().getUser().getAccount());
            mtp.setXm(xingming);
            mtp.setDm("半天妖");
            btyBxdaService.add(mtp);

            bxda = new BtyBxdaResult();
            bxda.setId(String.valueOf(mtp.getId()));
            bxda.setXm(xingming);
            bxda.setTel(account);
        }
//        if(!bxda.getXm().equals(xingming)){
//            BtyBxda bxdau = btyBxdaService.getById(bxda.getId());
//            bxdau.setXm(xingming);
//            btyBxdaService.updateById(bxdau);
//            bxda.setXm(xingming);
//        }
        model.addAttribute("bxda", bxda);

        return PREFIX + "/zxChengbenMingxi_ghs.html";
    }

    /**
     * 新增页面
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/zxChengbenMingxi_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/edit")
    public String edit(@RequestParam("btyZxKemuIdView") String btyZxKemuIdView, Model model) throws Exception {
        model.addAttribute("btyZxKemuIdView", URLDecoder.decode(btyZxKemuIdView, "UTF-8"));
        return PREFIX + "/zxChengbenMingxi_edit.html";
    }


    /**
     * 编辑页面-直接添加报销单的编辑
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/editNew")
    public String editNew(@RequestParam("btyZxKemuIdView") String btyZxKemuIdView, Model model) throws Exception {
        model.addAttribute("btyZxKemuIdView", URLDecoder.decode(btyZxKemuIdView, "UTF-8"));
        return PREFIX + "/zxChengbenMingxi_editNew.html";
    }

    /**
     * 供货商编辑
     * @param btyZxKemuIdView
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/editGhs")
    public String editGhs(@RequestParam("btyZxKemuIdView") String btyZxKemuIdView, Model model) throws Exception {
        model.addAttribute("btyZxKemuIdView", URLDecoder.decode(btyZxKemuIdView, "UTF-8"));
        String account =  LoginContextHolder.getContext().getUser().getAccount();
        String xingming =  LoginContextHolder.getContext().getUser().getName();
        BtyBxdaResult bxda = btyBxdaService.getByTel("半天妖",account);
        if(bxda ==null){
            BtyBxda mtp = new BtyBxda();
            mtp.setTel(LoginContextHolder.getContext().getUser().getAccount());
            mtp.setXm(xingming);
            mtp.setDm("半天妖");
            btyBxdaService.add(mtp);

            bxda = new BtyBxdaResult();
            bxda.setId(String.valueOf(mtp.getId()));
            bxda.setXm(xingming);
            bxda.setTel(account);
        }

//        if(!bxda.getXm().equals(xingming)){
//            BtyBxda bxdau = btyBxdaService.getById(bxda.getId());
//            bxdau.setXm(xingming);
//            btyBxdaService.updateById(bxdau);
//            bxda.setXm(xingming);
//        }
        model.addAttribute("bxda", bxda);
        return PREFIX + "/zxChengbenMingxi_editGhs.html";
    }

    /**
     * 审核
     *
     * @author zwf
     * @Date 2021-01-19
     */
    @RequestMapping("/shenhe")
    public String shenhe(@RequestParam("btyZxKemuIdView") String btyZxKemuIdView, Model model) throws Exception {
        model.addAttribute("btyZxKemuIdView", URLDecoder.decode(btyZxKemuIdView, "UTF-8"));
        return PREFIX + "/zxChengbenMingxi_shenhe.html";
    }

    /**
     * 新增接口
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ZxBxMingxiParam ZxBxMingxiParam) {
        ZxBxMingxiParam.setPinpai(pinpai);
        //设置登录名
        return this.zxBxMingxiService.add(ZxBxMingxiParam);
    }

    /**
     * 新增接口
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/batchTransStatus")
    @ResponseBody
    public ResponseData batchTransStatus(ZxBxMingxiParam para) {
        System.out.println(para.toString());
        String ids = para.getIds();
        String[] idsArr=  ids.split(",");
        for(String id:idsArr){
            Long idLong = Long.parseLong(id);
            ZxBxMingxi md = this.zxBxMingxiService.getById(idLong);
            md.setStatus(para.getStatus());
            this.zxBxMingxiService.updateById(md);
        }
        this.zxBxMingxiService.updateStatusNull();
        //设置登录名
        return ResponseData.success();
    }

    /**
     * 新增接口-直接添加报销单
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/addNewItem")
    @ResponseBody
    public ResponseData addNewItem(ZxBxMingxiParam ZxBxMingxiParam) {
        ZxBxMingxiParam.setPinpai(pinpai);
        return this.zxBxMingxiService.addNewItem(ZxBxMingxiParam);
    }

    /**
     * 新增接口-直接添加报销单
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/addNewItemGhs")
    @ResponseBody
    public ResponseData addNewItemGhs(ZxBxMingxiParam ZxBxMingxiParam) {
        ZxBxMingxiParam.setPinpai(pinpai);
        //判读是否维护银行卡和行号
        Long btyyBxdaKey = ZxBxMingxiParam.getBtyyBxdaKey();
        BtyBxda bxda =  this.btyBxdaService.getById(btyyBxdaKey);
        if (bxda.getYhk()==null || bxda.getYhk().equals("")) {
            return new ErrorResponseData("银行卡号未空，请在供货商信息中维护银行卡号！");
        }
        if (bxda.getBz()==null || bxda.getBz().equals("")) {
            return new ErrorResponseData("开户行联行行号未空，请在供货商信息中维护开户行联行行号！！");
        }
        return this.zxBxMingxiService.addNewItemGhss(ZxBxMingxiParam);
    }

    @RequestMapping("/addItemGhs")
    @ResponseBody
    public ResponseData addItemGhs(ZxBxMingxiParam ZxBxMingxiParam) {
        //判读是否维护银行卡和行号
        Long btyyBxdaKey = ZxBxMingxiParam.getBtyyBxdaKey();
        BtyBxda bxda =  this.btyBxdaService.getById(btyyBxdaKey);
        if (bxda.getYhk()==null || bxda.getYhk().equals("")) {
            return new ErrorResponseData("银行卡号未空，请在供货商信息中维护银行卡号！");
        }
        if (bxda.getBz()==null || bxda.getBz().equals("")) {
            return new ErrorResponseData("开户行联行行号未空，请在供货商信息中维护开户行联行行号！！");
        }
        Long userId = LoginContextHolder.getContext().getUser().getId();
        String msg = this.zxBxMingxiService.queryBxqk(userId,ZxBxMingxiParam.getHead().getBtyZxMdzxId(),ZxBxMingxiParam.getHuopinId());
        ZxBxMingxiParam.setPinpai(pinpai);

        ResponseData r = this.zxBxMingxiService.addNewItemGhs(ZxBxMingxiParam);
        r.setMessage(msg);
        return r;
    }

    /**
     * 编辑接口
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ZxBxMingxiParam ZxBxMingxiParam) {
        ZxBxMingxiParam.setPinpai(pinpai);
        this.zxBxMingxiService.update(ZxBxMingxiParam);
        return ResponseData.success();
    }



    /**
     *
     * 科目同步
     *
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/kemuTongbu")
    @ResponseBody
    public ResponseData kemuTongbu(ZxBxMingxiParam ZxBxMingxiParam) {
        ResponseData responseData = this.zxBxMingxiService.kemuTongbu(ZxBxMingxiParam);
        return responseData;
    }
    /**
     * 供货商修改
     *
     * @author zwf
     * @Date 2021-01-16
     *
     */
    @RequestMapping("/editItem2")
    @ResponseBody
    public ResponseData editItem2(ZxBxMingxiParam ZxBxMingxiParam) {
        ZxBxMingxiParam.setPinpai(pinpai);
        this.zxBxMingxiService.update(ZxBxMingxiParam);
        return ResponseData.success();
    }

    /**
     * 审核提交
     *
     * @author zwf
     * @Date 2021-01-18
     *
     */
    @RequestMapping("/shenheTijiao")
    @ResponseBody
    public ResponseData shenheTijiao(ZxBxMingxiParam ZxBxMingxiParam) throws Exception{
        ZxBxMingxiParam.setPinpai(pinpai);
        this.zxBxMingxiService.shenheTijiao(ZxBxMingxiParam);
        return ResponseData.success();
    }
    /**
     * 供货商修改
     *
     * @author zwf
     * @Date 2021-01-16
     *
     */
    @RequestMapping("/editItemGhs")
    @ResponseBody
    public ResponseData editItemGhs(ZxBxMingxiParam ZxBxMingxiParam) {
        ZxBxMingxiParam.setPinpai(pinpai);
        ResponseData responseData = this.zxBxMingxiService.updateGhs(ZxBxMingxiParam);
        return responseData;
    }

    /**
     * 删除接口
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ZxBxMingxiParam ZxBxMingxiParam) {
        boolean isDel = this.zxBxMingxiService.delete(ZxBxMingxiParam);
        if(isDel==false){
            return new ErrorResponseData("删除失败");
        }
        return ResponseData.success();
    }

    /**
     * 抽点
     *
     * @author zwf
     * @Date 2021-01-19
     */
    @RequestMapping("/choudian")
    @ResponseBody
    public ResponseData choudian(ZxBxMingxiParam ZxBxMingxiParam) {
        boolean isDel = this.zxBxMingxiService.choudian(ZxBxMingxiParam);
        return ResponseData.success();
    }

    /**
     * 抽点
     *
     * @author zwf
     * @Date 2021-01-19
     */
    @RequestMapping("/buchoudian")
    @ResponseBody
    public ResponseData buchoudian(ZxBxMingxiParam ZxBxMingxiParam) {
        boolean isDel = this.zxBxMingxiService.buchoudian(ZxBxMingxiParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/deleteGhs")
    @ResponseBody
    public ResponseData deleteGhs(ZxBxMingxiParam ZxBxMingxiParam) {
        boolean isDel = this.zxBxMingxiService.deleteGhs(ZxBxMingxiParam);
        if(isDel==false){
            return new ErrorResponseData("删除失败");
        }
        return ResponseData.success();
    }


    /**
     * 查看详情接口
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ZxBxMingxiParam ZxBxMingxiParam) {
        ZxBxMingxi detail = this.zxBxMingxiService.getById(ZxBxMingxiParam.getBtyZxChengbenMingxiId());
        if(detail.getStatus()==null || detail.getStatus().equals("")){
            detail.setStatus("2");
        }
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ZxBxMingxiParam ZxBxMingxiParam) {
        ZxBxMingxiParam.setPinpai(pinpai);
        return this.zxBxMingxiService.findPageBySpec(ZxBxMingxiParam);
    }
    /**
     * 投资情况表[明细]列表
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @ResponseBody
    @RequestMapping("/tzqkMingXiList")
    public LayuiPageInfo tzqkMingXiList(ZxBxMingxiParam ZxBxMingxiParam) {
        ZxBxMingxiParam.setPinpai(pinpai);
        ZxBxMingxiParam.setTzqk_flag("1");
        return this.zxBxMingxiService.findPageBySpec(ZxBxMingxiParam);
    }

    /**
     * 投资情况表[明细]列表
     *
     * @author jiangge
     * @Date 2020-11-19
     */
    @ResponseBody
    @RequestMapping("/queryCreateUser")
    public ResponseData queryCreateUser() {
        return this.zxBxMingxiService.queryCreateUser();
    }


    @RequestMapping("/obtainFromBaoxiaodan")
    @ResponseBody
    public ResponseData obtainFromBaoxiaodan(ZxTzqkViewResult param) {
        return this.zxBxMingxiService.obtainFromBaoxiaodan(param);
    }

    @RequestMapping("/btnImportMdbx")
    @ResponseBody
    public ResponseData btnImportMdbx(@RequestParam("file") MultipartFile file,@RequestParam("btyZxChengbenId") Long btyZxChengbenId){
        try {
            ResponseData result = this.zxBxMingxiService.btnImportMdbx(file,btyZxChengbenId);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return new ErrorResponseData("解析文件异常");
        }
    }


    /**
     * 投资情况-一键批量获取
     * @param param
     * @return
     */
    @RequestMapping("/createDakuandan")
    @ResponseBody
    public ResponseData onClickobtainAll(ZxBxMingxiParam param) {
        param.setPinpai(pinpai);
        return this.zxBxMingxiService.createDakuandan(param);
    }

    /**
     *  提交报销
     * @param param
     * @return
     */
    @RequestMapping("/tijiaobaoxiaoGhs")
    @ResponseBody
    public ResponseData tijiaobaoxiaoGhs(ZxBxMingxiParam param) {
        param.setPinpai(pinpai);
        return this.zxBxMingxiService.tijiaobaoxiaoGhs(param);
    }

    /**
     *  审批 单号的所有报销单
     * @param param
     * @return
     * by zwf 2021-01-20
     */
    @RequestMapping("/shenpiByDanhao")
    @ResponseBody
    public ResponseData shenpiByDanhao(ZxBxMingxiParam param) {
        param.setPinpai(pinpai);
        return this.zxBxMingxiService.shenpiByDanhao(param);
    }

    /**
     * 提交报销(被打回)
     * @param param
     * @return
     */
    @RequestMapping("/tijiaobaoxiaoGhsTh")
    @ResponseBody
    public ResponseData tijiaobaoxiaoGhsTh(ZxBxMingxiParam param) {
        param.setPinpai(pinpai);
        return this.zxBxMingxiService.tijiaobaoxiaoGhsTh(param);
    }

    @RequestMapping("/export")
    public ResponseEntity<byte[]> export(HttpServletRequest request, HttpServletResponse response, ZxBxMingxiParam param){
        return this.zxBxMingxiService.exportMx(request,response,param);
    }
}


